#include <cassert>
#include <iostream>
#include <unistd.h>

#include "database.h"

int main() {
    std::cout << "Hello World, this is my second homework in Kingsoft Cloud Summer Camp 2025." << "\n\n";
    const std::string test_db = "test_db";

    // 测试1：初始化数据库
    SimpleDatabase db(test_db);
    db.init();

    // 测试2：添加数据
    std::vector<Record> records_to_add = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };
    bool add_result = db.add(records_to_add);

    // 测试3：点查询
    Record found_rec;
    bool find_result = db.find_record_by_id(3, found_rec);

    // 测试4：范围查询
    auto range_records = db.find_records_in_range(2, 5);
    std::cout << "Found " << range_records.size() << " records:" << std::endl;

    // 测试5：前缀查询
    auto prefix_records = db.find_records_by_name_prefix("D");
    std::cout << "Found " << prefix_records.size() << " records:" << std::endl;

    // 测试6：更新名称
    bool update_result = db.update_name(2, "Robert");
    db.find_record_by_id(2, found_rec);

    // 测试7：删除记录
    bool delete_result = db.remove({1, 4});
    std::cout << "After deletion:" << std::endl;

    // 测试8：清空数据库
    bool truncate_result = db.truncate();
    std::cout << "After truncate, record count: "
            << db.find_records_in_range(0, 100).size() << std::endl;

    // 测试9：销毁数据库
    db.destory();
    // 重新初始化验证
    db.init();
    std::cout << "After init, record count: "
            << db.find_records_in_range(0, 100).size() << std::endl;


    //因为我写的析构函数主函数退出后会自动销毁数据库
    //所以如果想要查证文件是否写入data文件夹中，可以sleep在中途查看
    // sleep(100);

    //重启后再检验
    std::vector<Record> rta = {
        {1, "Alice", "Hello World"},
        {2, "Bob", "Hello World"},
        {3, "Charlie", "Hello World"},
        {4, "David", "Hello World"},
        {5, "Eve", "Hello World"}
    };
    bool ar = db.add(rta);

    auto rr = db.find_records_in_range(2, 5);
    std::cout << "Found " << rr.size() << " records:" << std::endl;

    return 0;
}
